package com.demo.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.demo.model.base.TableColumnInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder(value={"tableName_left","tableName_right","columnCount_left","columnCount_right","diffMap"},alphabetic = true)
public class TableStructureDiff {
	private String tableName_left;
	private String tableName_right;
	private Integer columnCount_left;
	private Integer columnCount_right;
	//key为列名，value为2个元素组成的集合
	private Map<String, List<TableColumnInfo>> diffMap;
	@JsonIgnore
	private Set<String> columnNameSet;
	
	@JsonIgnore
	private Map<String, TableColumnInfo> columnInfoMap_left;
	@JsonIgnore
	private Map<String, TableColumnInfo> columnInfoMap_right;
	@JsonIgnore
	private List<TableColumnInfo> columnInfoList_left;
	@JsonIgnore
	private List<TableColumnInfo> columnInfoList_right;
	public String getTableName_left() {
		return tableName_left;
	}
	public String getTableName_right() {
		return tableName_right;
	}
	public Integer getColumnCount_left() {
		return columnCount_left;
	}
	public Integer getColumnCount_right() {
		return columnCount_right;
	}
	public Map<String, List<TableColumnInfo>> getDiffMap() {
		return diffMap;
	}
	public Set<String> getColumnNameSet() {
		return columnNameSet;
	}
	public void addDataToCompare(String tableName_left,List<TableColumnInfo> columnInfoList_left,String tableName_right,List<TableColumnInfo> columnInfoList_right){
		this.tableName_left = tableName_left;
		this.tableName_right = tableName_right;
		this.setColumnInfoList_left(columnInfoList_left);
		this.setColumnInfoList_right(columnInfoList_right);
		this.genDiff();
	}
	
	private void genDiff() {
		diffMap = new HashMap<String, List<TableColumnInfo>>();
		for (Iterator<String> iterator = columnNameSet.iterator(); iterator.hasNext();) {
			String columnName = iterator.next();
			List<TableColumnInfo> list=new ArrayList<TableColumnInfo>(2);
			TableColumnInfo tableColumnInfo_left = columnInfoMap_left.get(columnName);
			TableColumnInfo tableColumnInfo_right = columnInfoMap_right.get(columnName);
			if (tableColumnInfo_left == null
					&& tableColumnInfo_right == null) {
				continue;
			}
			if (tableColumnInfo_left == null
					|| tableColumnInfo_right == null
					|| tableColumnInfo_left.getDataType().hashCode() != tableColumnInfo_right.getDataType().hashCode()
					|| tableColumnInfo_left.getDataLength().hashCode() != tableColumnInfo_right.getDataLength().hashCode()
					|| tableColumnInfo_left.getDataPrecision().hashCode() != tableColumnInfo_right.getDataPrecision().hashCode()
					|| tableColumnInfo_left.getDataDefault().hashCode() != tableColumnInfo_right.getDataDefault().hashCode()
					|| tableColumnInfo_left.getNullable().hashCode() != tableColumnInfo_right.getNullable().hashCode()
					) {
				list.add(tableColumnInfo_left==null?new TableColumnInfo():tableColumnInfo_left);
				list.add(tableColumnInfo_right==null?new TableColumnInfo():tableColumnInfo_right);
				diffMap.put(columnName, list);
			}
		}
		
	}
	
	private void setColumnInfoList_left(List<TableColumnInfo> columnInfoList_left) {
		this.columnInfoList_left = columnInfoList_left;
		columnInfoMap_left = new HashMap<String, TableColumnInfo>();
		if (columnNameSet == null) {
			columnNameSet = new HashSet<String>();
		}
		if (null != this.columnInfoList_left) {
			for (Iterator<TableColumnInfo> iterator = columnInfoList_left.iterator(); iterator
					.hasNext();) {
				TableColumnInfo tableColumnInfo = iterator.next();
				columnNameSet.add(tableColumnInfo.getColumnName());
				columnInfoMap_left.put(tableColumnInfo.getColumnName(), tableColumnInfo);
			}
			columnCount_left = columnInfoList_left.size();
		}
		
	}
	
	private void setColumnInfoList_right(List<TableColumnInfo> columnInfoList_right) {
		this.columnInfoList_right = columnInfoList_right;
		columnInfoMap_right = new HashMap<String, TableColumnInfo>();
		if (null != this.columnInfoList_right) {
			for (Iterator<TableColumnInfo> iterator = columnInfoList_right.iterator(); iterator
					.hasNext();) {
				TableColumnInfo tableColumnInfo = iterator.next();
				columnNameSet.add(tableColumnInfo.getColumnName());
				columnInfoMap_right.put(tableColumnInfo.getColumnName(), tableColumnInfo);
			}
			columnCount_right = columnInfoList_right.size();
		}		
	}
	
}
